﻿Public Class FrmHistoryMaintance
    Dim MyDataSet As DataSet = New DataSet()
    Private Sub FrmHistoryMaintance_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        InitParamter()
    End Sub
    Public Sub InitParamter()
        BeginTime.Value = DateTime.Now
        EndTime.Value = DateTime.Now
        Me.DataGridView1.ReadOnly = True
        Me.DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
        FillDataGrid("Select ID,MaintanceType,MaintanceName,MaintanceTime from MaintainaceHis" & " where MaintanceType=" & MaintanceSearchFlag)
    End Sub
    Public Sub FillDataGrid(ByVal sql As String)
        Try
            MyDataSet.Clear()
            MyDataSet = SearchMysql(sql)
            DataGridView1.DataSource = MyDataSet.Tables(0)
            DataGridView1.Columns("ID").HeaderText = "维护记录ID"
            DataGridView1.Columns("MaintanceType").HeaderText = "维护项目类型"
            DataGridView1.Columns("MaintanceName").HeaderText = "维护项目名称"
            DataGridView1.Columns("MaintanceTime").HeaderText = "上次维护时间"
            'DataGridView1.Columns("BoxId").Visible = False
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString() & ex.StackTrace.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub cmdDelete_Click(sender As Object, e As EventArgs) Handles cmdDelete.Click
        Try
            If DataGridView1.SelectedRows.Count >= 1 Then
                If MessageBox.Show("确定删除选定的记录吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
                    For i As Integer = 0 To DataGridView1.SelectedRows.Count - 1
                        Dim ID As Integer = Convert.ToInt32(DataGridView1.SelectedRows(i).Cells.Item("ID").Value.ToString())
                        If ID <> 0 Then
                            Dim sql As String = "DELETE FROM MaintainaceHis" & " WHERE ID = " & ID
                            HandleMysql(sql)
                        End If
                    Next
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                    'SaveDataGrid("Select ID,MaintanceType,MaintanceName,MaintanceTime from MaintainaceHis" & " where  MaintanceType=" & MaintanceSearchFlag, MyDataSet)
                    FillDataGrid("Select ID,MaintanceType,MaintanceName,MaintanceTime from MaintainaceHis" & " where  MaintanceType=" & MaintanceSearchFlag)
                    Me.DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically
                End If
            Else
                MessageBox.Show("没有选中任何行！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString() & ex.StackTrace.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub ReStart_Click(sender As Object, e As EventArgs) Handles ReStart.Click
        InitParamter()
    End Sub

    Private Sub cmdSearch_Click(sender As Object, e As EventArgs) Handles cmdSearch.Click
        If BeginTime.Value.Date < EndTime.Value.Date Then
            FillDataGrid("Select ID,MaintanceType,MaintanceName,MaintanceTime from MaintainaceHis" & " where MaintanceType=" & MaintanceSearchFlag & " and MaintanceTime>=" & "'" & BeginTime.Value.Date & "'" & " and MaintanceTime<=" & "'" & EndTime.Value.Date & "'")
        ElseIf BeginTime.Value.Date = EndTime.Value.Date Then
            FillDataGrid("Select ID,MaintanceType,MaintanceName,MaintanceTime from MaintainaceHis" & " where  MaintanceType=" & MaintanceSearchFlag & " and MaintanceTime>=" & "'" & BeginTime.Value.Date & "'")
        Else
            MessageBox.Show("开始时间不能大于结束时间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
End Class